package day08;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Demo02 {
    public static void main(String[] args) {
        JFrame f = new JFrame("捕鱼");
        f.setSize(800,480);//背景图大小
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MyPanel mp = new MyPanel();
        f.add(mp);
        f.setVisible(true);
        mp.move();
    }
}

class MyPanel extends JPanel{
    BufferedImage img;//定义一个图片变量
    BufferedImage fish13;
    int x = 400;
    int y = 100;
    int step = 3;
    public MyPanel(){
        //初始化加载图片到内存
        try {
            img = ImageIO.read(new File("images/bg.jpg"));
            fish13 = ImageIO.read(new File("images/fish13_00.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void paint(Graphics g){
        //绘制图片
        g.drawImage(img,0,0,null);
        g.drawImage(fish13,x,y,null);
    }

     //让鱼游动的放到
    // int step = 3;
    public void move(){
        while(true){
            x-=step;
            if(x<=-320){
                x = 800;
                y = (int)(Math.random()*480)-1;
                step = (int)(Math.random()*8);
            }
            try {
                Thread.sleep(1000/24);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint();
        }
    }

}
